using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace PFAGame
{
    public class Animation
    {
        // The image representing the collection of images used for animation
        Texture2D spriteStrip;
        // The time since we last updated the frame
        int elapsedTime;
        // The time we display a frame until the next one
        public int frameTime;
        // The number of frames that the animation contains
        int frameCount;
        // The index of the current frame we are displaying
        int currentFrame;
        // The area of the image strip we want to display
        Rectangle sourceRect = new Rectangle();
        // Width of a given frame
        int FrameWidth;
        // Height of a given frame
        int FrameHeight;
        // The state of the Animation
        public bool Active;
        // Determines if the animation will keep playing or deactivate after one run
        bool Looping;

        public void Initialize(Texture2D texture,
                                int frameWidth, int frameHeight, int frameCount,
                                int frametime, bool looping)
        {
            // Keep a local copy of the values passed in
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.frameCount = frameCount;
            this.frameTime = frametime;

            Looping = looping;
            spriteStrip = texture;

            // Set the time to zero
            elapsedTime = 0;
            currentFrame = 0;

            // Set the Animation to active by default
            Active = true;
            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
        }

        public void reset()
        {
            elapsedTime = 0;
            currentFrame = 0;
            Active = true;
        }

        public void Update(GameTime gameTime)
        {
            // Do not update the game if we are not active
            if (Active == false)
                return;

            // Update the elapsed time
            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            // If the elapsed time is larger than the frame time
            // we need to switch frames
            if (elapsedTime > frameTime)
            {
                // Move to the next frame
                ++currentFrame;


                // If the currentFrame is equal to frameCount reset currentFrame to zero
                if (currentFrame == frameCount)
                {
                    // If we are not looping deactivate the animation
                    if (Looping == false)
                    {
                        Active = false;
                        --currentFrame;
                    }
                    else
                        currentFrame = 0;
                }


                // Reset the elapsed time to zero
                elapsedTime = 0;
            }


            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
        }

        // Draw the Animation Strip
        public void Draw(SpriteBatch spriteBatch, Rectangle destinationRect, Color color)
        {
            spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
        }

    }
}
